
import { examGetInfo } from '@/api/common'
import { Loading } from 'element-ui';
import store from '@/store'

let loadingInstance = null;

const xmgl = {
    state: {
        xmxx: {},
        examPermissions: null,
        examSubjects: null,
    },
    mutations: {
        SET_PROJECT_INFO: (state, xmxx) => {
            state.xmxx = xmxx
        },
        SET_EXAM_PERMISSIONS: (state, examPermissions) => {
            state.examPermissions = examPermissions
        },
        SET_EXAM_SUBJECTS: (state, examSubjects) => {
            state.examSubjects = examSubjects
        }
    },
    actions: {
        getXmxx: ({ commit, state }) => {
            const roles = store.getters && store.getters.roles;
            let isAdmin = roles.some(v => {
                return "admin" === v
            })
            if (state.xmxx.id && (isAdmin || state.examPermissions)) {
                return state.xmxx
            };
            let xmxx = localStorage.getItem("xmxx");
            if (xmxx) {
                xmxx = JSON.parse(xmxx)
                commit('SET_PROJECT_INFO', xmxx);

                return new Promise((resolve, reject) => {
                    if (!isAdmin) {
                        loadingInstance = Loading.service({ fullscreen: true });
                        examGetInfo({ projectId: xmxx.id }).then((res) => {
                            commit('SET_EXAM_PERMISSIONS', res.permissions)
                            commit('SET_EXAM_SUBJECTS', res.subjects)
                            resolve(xmxx)
                        }).catch(error => {
                            window.location.href = "/xmgl";
                            reject(error)
                        }).finally(() => {
                            loadingInstance.close();
                        })
                    } else {
                        resolve(xmxx)
                    }
                })

            } else {
                window.location.href = "/xmgl";
            }
        },
        setXmxx: ({ commit }, xmxx) => {
            localStorage.setItem("xmxx", JSON.stringify(xmxx));
            commit('SET_PROJECT_INFO', xmxx);
            const roles = store.getters && store.getters.roles;
            let isAdmin = roles.some(v => {
                return "admin" === v
            })
            return new Promise((resolve, reject) => {
                if (!isAdmin) {
                    loadingInstance = Loading.service({ fullscreen: true });
                    examGetInfo({ projectId: xmxx.id }).then((res) => {
                        commit('SET_EXAM_PERMISSIONS', res.permissions)
                        commit('SET_EXAM_SUBJECTS', res.subjects)
                        resolve()
                    }).catch(() => {
                        reject();
                    }).finally(() => {
                        loadingInstance.close();
                    })
                } else {
                    resolve()
                }
            })
        }
    }
}

export default xmgl